// JavaScript Document
var UPLOAD = function(){
	var upload = function(){
		this.container = null;
		this.size = 0;
		this.index = [];
	}
	upload.prototype = {
		init:function(container){
			var self = this;
			self.container = container;
		},
		show:function(files){
			var self = this;
			if(files == null){
				return;
			}
			var index = self.size;
			self.size = self.size + files.length;			
			var sum = document.getElementById("sum");
			var i ;
			for(var i = 0;i<30-index&&i<files.length;i++){
				var file = files[i];
				//self.files.push(file);
				var reader = new FileReader();
				
				//监听文件读取结束后事件
				reader.onloadend = function (e) {
					//将图片添加到显示列表
					var section = document.createElement("section");
					section.className = "picutreContainer";
					var photo = document.createElement("img");
					photo.src = e.target.result;
					photo.className = "photo";
					var hook = document.createElement("img");
					hook.src = "/networkforum/images/hook.jpg";
					hook.className = "hook";
					section.appendChild(hook);
					section.appendChild(photo);
					self.container.appendChild(section);
					var inde = index++;
					photo.onclick = function(ev){
						if(hook.style.display == "none"){
							hook.style.display = "";
							for(var j =0;j<self.index.length;j++){
								if(inde == self.index[j]){
									self.index.splice(j,1);
									break;
								}	
							}							
						}
						else{
							hook.style.display = "none";
							self.index.push(inde);
						}
					}
				}
				reader.readAsDataURL(file);
			}
			
			sum.innerHTML = "总共"+(self.size>30?30:self.size)+"张";
			if(i<files.length){
				alert("每次上传不超过30张图片！");
				return;
			}
		},
	}
	return new upload();
}
var upload = UPLOAD();
function show(ev){
	if(upload.size>=30){
		alert("最多选中30张图片!");
		return;
	}
	ev = ev || window.event;
	var files = ev.target.files;
	
	upload.init(document.getElementById("pictureShow"));
	upload.show(files);
	
	addFileInput(ev.target);
}
/**
 * 添加新的文件选择控件并把以前的隐藏
 * @param old
 * @return
 */
function addFileInput(old){
	old.className = "";
	old.id = "";
	old.style.display = "none";
	old.style.zIndex = "-1px;";
	
	var fileInput = document.createElement("input");
	fileInput.className = "file";
	fileInput.id = "file";
	fileInput.onchange = show;
	fileInput.multiple="multiple";
	fileInput.name = "file";
	fileInput.type = "file";
	document.getElementById("fileLink").appendChild(fileInput);
}
/**
 * 把选中的文件上传到服务器
 * @param obj
 * @return
 */
function uploadHandle(obj){
	var file = document.getElementsByName("file");
	if(file.length<2){
		alert("请先选择图片");
		return false;
	}
	
	var arr = upload.index;
	document.getElementById("index").value = arr;
	obj.action = "/networkforum/picture/upload_picture.do";
	document.getElementById("uploadBack").style.display = "";
	document.getElementById("pictureShow").innerHTML = "";
	return true;
}
function groupNameHandle(dis){
	var section = document.getElementById("groupinner");
	if(section.style.display == ""){
		section.style.display = "none";
	}
	else{
		section.style.display = "";		
	}
	
	var fileLink = document.getElementById("fileLink");
	var len = fileLink.children.length
	while(len>2){
		fileLink.removeChild(len-1);
		len--;
	}
}
function showGroups(data){
	var ul = document.getElementById("groupUl");
	var groupName = document.getElementById("groupName");
//	data = eval("("+data+")");
	data = toJson(data);
	var frag = document.createDocumentFragment();
	for(var i = 0;i<data.length;i++){
		var d = data[i];
		var li = document.createElement("li");
		li.innerHTML = d.name;
		li.onclick = function(ev){
			ev = ev||window.event;
			groupName.value = ev.target.innerHTML;
			document.getElementById("groupinner").style.display = "none";
		}
		frag.appendChild(li);
	}
	ul.appendChild(frag);
}
window.onload = function(){
	//登录页面后通过myajax获取进一步的信息
	var myajax = getByAjax();
	myajax.init(null,"get",true);
	myajax.get("/networkforum/picture/get_groups.do",showGroups);

}

